﻿using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;

namespace BW.Mes.WinForms.Process
{
    public partial class ProcessUpdateForm : DevExpress.XtraEditors.XtraForm
    {
        private readonly HttpClient _httpClient;
        private readonly string _processApiBaseUrl = "https://localhost:7222";
        private ProcessDataForm.ProcessModel _currentModel; // 保存当前编辑的模型
        public ProcessUpdateForm()
        {
            InitializeComponent();
            _httpClient = new HttpClient();
            _httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
        }
        /// <summary>
        /// 构造函数 
        /// </summary>
        /// <param name="model"></param>
        public ProcessUpdateForm(ProcessDataForm.ProcessModel model)
        {
            InitializeComponent();
            _currentModel = model; // 保存模型引用
            _httpClient = new HttpClient();
            _httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            if (model != null)
            {
                textEdit1.Text = model.ProcessName;
                textEdit2.Text = model.ProcessExplain;
                textEdit3.Text = model.ProcessDesc;
                textEdit4.Text = model.ProcessHead;
                comboBoxEdit1.Text = model.ProcessDepartment;
                comboBoxEdit2.Text = model.Processworkshop;
                textEdit7.Text = model.ProcessMaterials;
                radioGroup1.SelectedIndex = model.ProcessadEnable ? 1 : 0;
                // 其他字段
            }
        }


        public class ProcessModel
        {
            public long Id { get; set; }
            public string processName { get; set; }
            public string processExplain { get; set; }
            public string processDesc { get; set; }
            public string processHead { get; set; }
            public string processDepartment { get; set; }
            public string processworkshop { get; set; }
            public string processMaterials { get; set; }
            public bool processadEnable { get; set; }
        }
        /// <summary>
        /// 修改实体
        /// </summary>
        public class ProcessUpdateModel
        {
            public long Id { get; set; }
            public string processName { get; set; }
            public string processExplain { get; set; }
            public string processDesc { get; set; }
            public string processHead { get; set; }
            public string processDepartment { get; set; }
            public string processworkshop { get; set; }
            public string processMaterials { get; set; }
            public bool processadEnable { get; set; }
        }

        private void ProcessUpdateForm_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 修改按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            // 验证必填字段
            if (string.IsNullOrWhiteSpace(textEdit1.Text))
            {
                XtraMessageBox.Show("请输入工序名称！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textEdit1.Focus();
                return;
            }

            // 构建更新模型
            var updateModel = new ProcessUpdateModel
            {
                Id = _currentModel.Id, // 使用当前模型的ID
                processName = textEdit1.Text.Trim(),
                processExplain = textEdit2.Text.Trim(),
                processDesc = textEdit3.Text.Trim(),
                processHead = textEdit4.Text.Trim(),
                processDepartment = comboBoxEdit1.Text,
                processworkshop = comboBoxEdit2.Text,
                processMaterials = textEdit7.Text.Trim(),
                processadEnable = radioGroup1.SelectedIndex == 1 // 1对应"是"，0对应"否"
            };

            // 发送更新请求
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(_processApiBaseUrl);

                // 创建JSON数据
                var json = JsonConvert.SerializeObject(updateModel);
                var content = new StringContent(json, Encoding.UTF8, "application/json");

                // 发送PUT请求进行更新
                var response = await client.PostAsync("/api/ProcessModel/UpdateProcessModel", content);

                if (response.IsSuccessStatusCode)
                {
                    XtraMessageBox.Show("更新成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    var errorMessage = await response.Content.ReadAsStringAsync();
                    XtraMessageBox.Show($"更新失败：{response.ReasonPhrase}\n{errorMessage}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            // 重置为原始数据
            if (_currentModel != null)
            {
                textEdit1.Text = _currentModel.ProcessName;
                textEdit2.Text = _currentModel.ProcessExplain;
                textEdit3.Text = _currentModel.ProcessDesc;
                textEdit4.Text = _currentModel.ProcessHead;
                comboBoxEdit1.Text = _currentModel.ProcessDepartment;
                comboBoxEdit2.Text = _currentModel.Processworkshop;
                textEdit7.Text = _currentModel.ProcessMaterials;
                radioGroup1.SelectedIndex = _currentModel.ProcessadEnable ? 1 : 0;
            }
        }
    }
}